package com.example.lowbird.lowbird.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;


import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.app.Myapp;
import com.example.lowbird.lowbird.bean.Logrbean;
import com.example.lowbird.lowbird.bean.User;
import com.example.lowbird.lowbird.constant.Contants;
import com.example.lowbird.lowbird.java.SHopcar;
import com.example.lowbird.lowbird.utils.Dbutils;
import com.example.lowbird.lowbird.utils.SpUtil;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class GoodActivity extends AppCompatActivity {

    private WebView wv_good;
    private Context mContext;
    private SHopcar shopcar;
    private int id2;
    private WebAppInterface webAppInterface;
    private ImageView iv_share;
    private String string;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good);
        mContext = this;

        Intent intent = getIntent();
        shopcar = ((SHopcar) intent.getSerializableExtra("shopcar"));
        if (shopcar == null) {

            this.finish();
        }

        initview();
    }

    private void initview() {
        wv_good = ((WebView) findViewById(R.id.wv_good));

        iv_share = ((ImageView) findViewById(R.id.iv_share));
        setiv_share();

        wv_good.getSettings().setJavaScriptEnabled(true);


        wv_good.setWebChromeClient(new WebChromeClient() {

            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {


                return true;
            }
        });
        wv_good.loadUrl(Contants.API.WARES_DETAIL);

        webAppInterface = new WebAppInterface();
        wv_good.addJavascriptInterface(webAppInterface, "appInterface");
        wv_good.setWebViewClient(new Mywebview());

    }

    private void setiv_share() {
//iv_share.setOnClickListener(new View.OnClickListener() {
//    @Override
//    public void onClick(View v) {
//        ShareUtils.showShare(mContext,shopcar.getName(),shopcar.getImageurl(),
//                shopcar.getName()+"",shopcar.imageurl,shopcar.getShopurl(),shopcar.imageurl);
//    }
//});


    }

    class Mywebview extends WebViewClient {
        @Override
        public void onPageFinished(WebView view, String url) {
            webAppInterface.showDetail();

        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        /**
         * 判断用户点击的是返回实体键，并且WebView可以返回到上一页
         */
        if (keyCode == KeyEvent.KEYCODE_BACK && wv_good.canGoBack()) {
            wv_good.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    class WebAppInterface {

        @JavascriptInterface
        public void showDetail() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    wv_good.loadUrl("javascript:showDetail(" + shopcar.getId2() + ")");

                }
            });

        }

        @JavascriptInterface
        public void buy(int id) {


            Log.e("GOD", "addToCart购买 ");

            addtofavourite();

        }

        private void addtofavourite() {


            User user = Myapp.getmInsance().getuser();

            if (user == null) {

                Intent intent = new Intent(mContext,RegistActivity.class);
                startActivityForResult(intent,1);
            }
            else {

                long id = user.getId();

                Map<String, String> params1 = new HashMap<>(1);
                params1.put("user_id", id+"");

                params1.put("ware_id",id2+"");

                OkHttpUtils.post().url(Contants.API.FAVORITE_CREATE).
                        params(params1).build().execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("FAV", "onResponse1: "+e.toString());
                    }

                    @Override
                    public void onResponse(String response, int id) {


                        Log.e("FAV", "onResponse2: "+response.toString() );
                    }
                });



            }
        }

        private void getuserimage() {

            String user = SpUtil.getString(mContext, string, "");

            Logrbean logrbean = new Gson().fromJson(user, Logrbean.class);

            int id = logrbean.getData().getId();

        }

        @JavascriptInterface
        public void addToCart(int id) {
            Dbutils.createShop(shopcar);
            Log.e("GOD", "addToCart:收藏 ");

        }


    }

}
